//
//  LNHGoodsParametersPopCell.m
//  LNH
//
//  Created by cruise on 2018/2/10.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "LNHGoodsParametersPopCell.h"
#import <TTGTagCollectionView/TTGTextTagCollectionView.h>

#import "LNHGoodsDetailViewModel.h"

@interface LNHGoodsParametersPopCell () <TTGTextTagCollectionViewDelegate>

@end

@implementation LNHGoodsParametersPopCell {
    TTGTextTagConfig *_config;
    NSInteger _selectedRow;
    LNHGoodsDetailViewModel *_viewModel;
}

-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _previousIndex = 0;
        
        _config = [[TTGTextTagConfig alloc] init];
        [_config setTagTextFont:[UIFont systemFontOfSize:12]];
        [_config setTagTextColor:RGBCOLOR(51, 51, 51)];
        [_config setTagSelectedTextColor:[UIColor whiteColor]];
        [_config setTagBackgroundColor:RGBCOLOR(245, 245, 245)];
        [_config setTagSelectedBackgroundColor:RGBCOLOR(236, 119, 66)];
        [_config setTagBorderWidth:0];
        [_config setTagCornerRadius:15];
        [_config setTagSelectedCornerRadius:15];
        [_config setTagShadowOpacity:0];
        [_config setTagExtraSpace:(CGSize){ 30, 15 }];
        [_config setTagMaxWidth:0.0f];
        
        [self.contentView addSubview:self.label];
        [self.contentView addSubview:self.tagView];
        
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(10);
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.height.mas_equalTo(20);
        }];
        
        [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.label.mas_bottom).offset(10);
            make.left.right.equalTo(self.label);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
        }];
        
        
    }
    return self;
}

-(void) configureForModel:(id) viewModel selectedRow:(NSInteger)selectedRow{
    _selectedRow = selectedRow;
    
    if ([viewModel isKindOfClass:[LNHGoodsDetailViewModel class]]){
        
        _viewModel = viewModel;
        
        NSDictionary *dict = _viewModel.parameterList[selectedRow];
        NSArray *tags = dict[kPropertyDataKeyGoodsDetailSkuAttrValueStringList];
        
        [self.label setText:dict[kPropertyDataKeyGoodsDetailSkuName]];
        
        [self.tagView removeAllTags];
        [self.tagView addTags:tags withConfig:_config];
        
        
        [self.tagView setTagAtIndex:self.previousIndex selected:YES];
        
    }
}

-(UILabel *) label {
    if (!_label) {
        _label = [UILabel new];
        [_label setTextColor:RGBCOLOR(51, 51, 51)];
        [_label setFont:[UIFont systemFontOfSize:14]];
    }
    return _label;
}

-(TTGTextTagCollectionView *) tagView {
    if (!_tagView) {
        _tagView = [[TTGTextTagCollectionView alloc] init];
        [_tagView setDelegate:self];
        _tagView.manualCalculateHeight = YES;
//        [_tagView setSelectionLimit:1];
    }
    return _tagView;
}

- (BOOL)textTagCollectionView:(TTGTextTagCollectionView *)textTagCollectionView canTapTag:(NSString *)tagText atIndex:(NSUInteger)index currentSelected:(BOOL)currentSelected {
    
    [self.tagView setTagAtIndex:self.previousIndex selected:NO];
    
    return YES;
}

- (void)textTagCollectionView:(TTGTextTagCollectionView *)textTagCollectionView didTapTag:(NSString *)tagText atIndex:(NSUInteger)index selected:(BOOL)selected{
    _previousIndex = index;
    
    [_viewModel skuSelectedRow:_selectedRow atIndex:index];
}

@end
